﻿using System.Collections.Generic;
using RabbitMQ.Client;

namespace Fast.Extensions.RabbitMQ
{
    /// <summary>
    /// 队列申明配置
    /// </summary>
    [SkipScan]
    public class QueueDeclareConfiguration
    {
        /// <summary>
        /// 队列名称
        /// </summary>
        public string QueueName { get; set; }

        /// <summary>
        /// 是否持久的
        /// </summary>
        public bool Durable { get; set; }

        /// <summary>
        /// 是否排他
        /// 1.声明了exclusive属性的队列只对首次声明它的连接可见，并且在连接断开时自动删除
        /// 2.针对连接可见，只要是当前connection下的信道都可以访问
        /// 3.一旦该队列被声明，其他连接无法声明相同名称的排他队列，其他连接的通道也无法绑定此队列
        /// 4.队列即使显示声明为durable，连接断开时（注意不是信道断开）也会被自动删除
        /// </summary>
        public bool Exclusive { get; set; }

        /// <summary>
        /// 是否自动删除
        /// </summary>
        public bool AutoDelete { get; set; }

        /// <summary>
        /// 列表相关参数
        /// </summary>
        public IDictionary<string, object> Arguments { get; }

        public QueueDeclareConfiguration(
            string queueName,
            bool durable = true,
            bool exclusive = false,
            bool autoDelete = false)
        {
            QueueName = queueName;
            Durable = durable;
            Exclusive = exclusive;
            AutoDelete = autoDelete;
            Arguments = new Dictionary<string, object>();
        }

        public virtual QueueDeclareOk Declare(IModel channel)
        {
            return channel.QueueDeclare(
                queue: QueueName,
                durable: Durable,
                exclusive: Exclusive,
                autoDelete: AutoDelete,
                arguments: Arguments
            );
        }
    }
}